package com.example.property

import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.common.mvi.BaseMviActivity
import com.example.path.RouterPath
import com.example.property.databinding.ActivityParkDetailBinding
import com.example.property.entity.CarApplyValues
import com.google.vr.sdk.widgets.pano.VrPanoramaEventListener
import com.google.vr.sdk.widgets.pano.VrPanoramaView

import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch

/**
 * 预览车位详情VR模拟
 * */
@Route(path = RouterPath.PATH_DETAIL)
@AndroidEntryPoint
class ParkDetailActivity : BaseMviActivity<ActivityParkDetailBinding>() {

    @Autowired
    lateinit var carApplyValues:CarApplyValues

    override fun getLayoutId(): Int =R.layout.activity_park_detail

    override fun initViews() {
        ARouter.getInstance().inject(this)
        mDataBinding.includeDetail.tvTitle.text="车位预览"
        mDataBinding.includeDetail.ivBack.setOnClickListener {
           this@ParkDetailActivity.finish()
        }
        //选择
        mDataBinding.tvSelsected.setOnClickListener {
            ARouter.getInstance().build(RouterPath.PATH_SELECT_PARK)
                .withSerializable("carApplyValues",carApplyValues)
                .navigation()
        }


        //初始化设置预览参数
        mDataBinding.vrPanorama.setInfoButtonEnabled(false)
        mDataBinding.vrPanorama.setFullscreenButtonEnabled(false)
        //设置Vr加载事件监听
        mDataBinding.vrPanorama.setEventListener(object : VrPanoramaEventListener(){
            override fun onLoadError(errorMessage: String) {
                Log.i("=========", "onLoadError: ${errorMessage}")
            }
            override fun onLoadSuccess() {
                Log.i("=========", "onLoadSuccess: ")
            }
        })
        //加载车位全景图片
        loadImageFromAssets()

    }

    /**
     * 从assets目录加载图片
     * */
    private fun loadImageFromAssets(){
        lifecycleScope.launch(Dispatchers.IO){
            try {
                var assetsManger = assets
                var inputStream = assetsManger.open("cc.jpg")
                var bitmap = BitmapFactory.decodeStream(inputStream)
                var pannoOptions = VrPanoramaView.Options()
                pannoOptions.inputType=VrPanoramaView.Options.TYPE_MONO
                mDataBinding.vrPanorama.loadImageFromBitmap(bitmap,pannoOptions)
            } catch (e: Exception) {
               e.printStackTrace()
            }finally {
                cancel()
            }
        }

    }

    override fun onPause() {
        super.onPause()
        mDataBinding.vrPanorama.pauseRendering()
    }

    override fun onResume() {
        super.onResume()
        mDataBinding.vrPanorama.resumeRendering()
    }

    override fun onDestroy() {
        mDataBinding.vrPanorama.shutdown()
        super.onDestroy()
    }



}